package com.coder4j.utils;

import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * 规定统一的MD5加密方法
 * @author Keller
 * @date 2020-04-20 11:11:42
 *
 */
public class Md5Utils {
    private static String charset = "UTF-8";
    /**
     * 将普通字符串用md5加密
     * @param str
     * @return
     */
    public static String getMd5String(String str) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("md5");
            byte[] result = md5.digest(str.getBytes(charset));
            String md5code = new BigInteger(1, result).toString(16);
            for (int i = 0; i < 32 - md5code.length(); i++) {
                md5code = "0" + md5code;
            }
            return md5code.toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws Exception{
        String str = "你好";
        String md5Str = getMd5String(str);
        System.out.println(md5Str);
    }

}
